package bb.lanxing.util.map;

import java.util.ArrayList;

import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.FragmentActivity;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.config.Constants;
import bb.lanxing.fragment.BaiduMapFragment;
import bb.lanxing.fragment.base.BaseMapFragment;
import bb.lanxing.lib.widget.utils.Density;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.DistanceUtil;
import bb.lanxing.util.MapUtil;

public class MapViewUtil {
    public static void drawAltitude(final MapDrawSupport mapDrawSupport, final LatLng mapPoint, final LatLng earth,
                                    final TextView info) {
        final BaseMapFragment mapFragment = mapDrawSupport.getMapFragment();
        if (mapFragment == null || !mapFragment.isAdded()) {
            return;
        }
        final FragmentActivity activity = mapDrawSupport.getActivity();
        Drawable drawable =
                ResourcesCompat.getDrawable(activity.getResources(), R.drawable.map_red_marker, activity.getTheme());
        if (drawable == null) {
            return;
        }
        int dp2px = Density.dp2px(activity, 8.0f);
        drawable.setBounds(0, 0, dp2px, dp2px);
        final Object drawMarker = mapFragment.drawMarker(BaseMapFragment.OVERLAY_TYPE_ALTITUDE, mapPoint, drawable,
                (String) null, 0.5f, 0.5f);
        MapUtil.requestAltitude(earth.latitude, earth.longitude, new MapUtil.RequestDataListener<Double>() {
            @Override
            public void onResult(Double altitude) {
                if (mapDrawSupport.isAltitudeOpen()) {
                    View inflate =
                            activity.getLayoutInflater().inflate(R.layout.map_altidute_with_text, (ViewGroup) null);
                    TextView textView = (TextView) inflate.findViewById(R.id.nameView);
                    textView.setVisibility(View.VISIBLE);
                    final String str = altitude.intValue() + "m";
                    TextView textView2 = info;
                    if (textView2 != null) {
                        textView2.post(new Runnable() {
                            @Override
                            public void run() {
                                info.setText(App.getContext().getString(R.string.map_tool_altitude_info, str));
                            }
                        });
                    }
                    textView.setText(str);
                    mapFragment.drawMarker(BaseMapFragment.OVERLAY_TYPE_ALTITUDE, mapPoint, inflate, 0.5f, 0.79f);
                    mapFragment.removeOverlay(drawMarker, 4);
                }
            }
        });
    }

    public static void drawDistance(final MapDrawSupport mapDrawSupport, final LatLng mapPoint, LatLng earth,
            TextView info) {
        LatLng common2Earth = null;
        final BaseMapFragment mapFragment = mapDrawSupport.getMapFragment();
        if (mapFragment == null || !mapFragment.isAdded()) {
            return;
        }
        FragmentActivity activity = mapDrawSupport.getActivity();
        if (mapDrawSupport.getLastDistancePoint() == null) {
            mapDrawSupport.setDistanceSum(Utils.DOUBLE_EPSILON);
            final Drawable drawable = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.map_red_marker,
                    activity.getTheme());
            if (drawable == null) {
                return;
            }
            int dp2px = Density.dp2px(activity, 8.0f);
            drawable.setBounds(0, 0, dp2px, dp2px);
            activity.runOnUiThread(() -> mapFragment.drawMarker(BaseMapFragment.OVERLAY_TYPE_DISTANCE, mapPoint,
                    drawable, null, 0.5f, 0.5f));
        } else {
            LatLng lastDistancePoint = mapDrawSupport.getLastDistancePoint();
            if (mapFragment instanceof BaiduMapFragment) {
                common2Earth = BiCiCoorConverter.baidu2Earth(lastDistancePoint);
            } else {
                // common2Earth = mapFragment instanceof OsmMapFragment ?
                // BiCiCoorConverter.common2Earth(lastDistancePoint) : lastDistancePoint;
            }
            double distanceSum = mapDrawSupport.getDistanceSum() + DistanceUtil.get(common2Earth, earth);
            String formatDistance = MapUtil.formatDistance(distanceSum);
            if (info != null) {
                info.setText(App.getContext().getString(R.string.map_tool_distance_info, formatDistance));
            }
            mapDrawSupport.setDistanceSum(distanceSum);
            View inflate = activity.getLayoutInflater().inflate(R.layout.map_altidute_with_text, (ViewGroup) null);
            TextView textView = (TextView) inflate.findViewById(R.id.nameView);
            inflate.findViewById(R.id.pointView).setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);
            textView.setText(formatDistance);
            ArrayList<LatLng> arrayList = new ArrayList<>(2);
            arrayList.add(lastDistancePoint);
            arrayList.add(mapPoint);
            mapFragment.drawLine1(BaseMapFragment.OVERLAY_TYPE_DISTANCE, arrayList, Constants.MAP_DEFAULT_LINE_COLOR,
                    true);
            mapFragment.drawMarker(BaseMapFragment.OVERLAY_TYPE_DISTANCE, mapPoint, inflate, 0.5f, 0.79f);
        }
        mapDrawSupport.setLastDistancePoint(mapPoint);
    }
}